package com.binhthuan.catechist.client.menu;

import com.binhthuan.catechist.client.ECUser;
import com.google.gwt.event.shared.HandlerRegistration;
import com.smartgwt.client.types.SelectionStyle;
import com.smartgwt.client.types.TreeModelType;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.layout.SectionStackSection;
import com.smartgwt.client.widgets.tree.Tree;
import com.smartgwt.client.widgets.tree.TreeGrid;
import com.smartgwt.client.widgets.tree.TreeNode;
import com.smartgwt.client.widgets.tree.events.HasLeafClickHandlers;
import com.smartgwt.client.widgets.tree.events.LeafClickHandler;

public class StackMenuSection extends SectionStackSection implements HasLeafClickHandlers {

    public static String ROOT_PATH = "/";
    private TreeGrid treeGrid;

    public StackMenuSection(String title) {
        this(title, (StackMenuItem[]) null);
    }

    public StackMenuSection(String title, String icon) {
        this("</span>" + Canvas.imgHTML(icon) + title + "</span>", (StackMenuItem[]) null);
    }

    public StackMenuSection(String title, StackMenuItem... menuItems) {
        super(title);

        treeGrid = new TreeGrid();
        treeGrid.setWidth100();
        treeGrid.setHeight100();
        treeGrid.setNodeIcon(StackMenuItem.DEFAULT_ICON);
        treeGrid.setShowHeader(false);
        treeGrid.setLeaveScrollbarGap(false);
        treeGrid.setShowAllRecords(true);
        treeGrid.setSelectionType(SelectionStyle.SINGLE);

        Tree data = new Tree();
        data.setModelType(TreeModelType.CHILDREN);

        treeGrid.setData(data);
        addItem(treeGrid);
        addMenuItems(menuItems);
    }

    public StackMenuSection(String icon, String title, StackMenuItem... menuItems) {
        this(Canvas.imgHTML(icon, 24, 24) + title);
    }

    public void addMenuItem(StackMenuItem item) {
        if (item != null && item.getFactory().checkCertificated(ECUser.getSecurityCode())) {
            treeGrid.getData().add(item, ROOT_PATH);
        }
    }

    public void addMenuItems(StackMenuItem... items) {
        if (items != null) {
            for (StackMenuItem item : items) {
                addMenuItem(item);
            }
        }
    }

    @Override
    public HandlerRegistration addLeafClickHandler(LeafClickHandler handler) {
        return ensureTreeGrid().addLeafClickHandler(handler);
    }

    private TreeGrid ensureTreeGrid() {
        if (treeGrid == null) {
            treeGrid = new TreeGrid();
        }
        return treeGrid;
    }

    public void selectMenuItem(StackMenuItem selectedItem) {
        treeGrid.deselectAllRecords();
        treeGrid.selectRecord(selectedItem);
    }

    public StackMenuItem getMenuItem(String name) {
        TreeNode[] nodes = treeGrid.getData().getAllNodes();

        for (TreeNode node : nodes) {
            if (node.getName().equals(name)) {
                return (StackMenuItem) node;
            }
        }

        return null;
    }

    public int getItemCount() {
        return treeGrid.getData().getAllNodes().length;
    }
}
